﻿using System;
using System.Collections.Generic;
using AccountModule;
using Framework;
using RecipeModule;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class MyRecipeView : ViewProxy
    {
        private GameObject[] _bgs;
        private MyRecipeGrid[] _recipeGrids;

        private ClickTrigger _triggerOrder;
        private ClickTrigger _triggerHot;
        private ClickTrigger _triggerInfo;

        private Text _textMoney;
        
        protected override void SetupView()
        {
            LoadRes("Views/Recipe/MyRecipeView");
        }

        protected override void OnInit()
        {
            _bgs = new GameObject[3];
            for (int i = 0; i < 3; ++i)
            {
                GameObject bg = _transform.Find("Bgs/Bg_" + (i + 1)).gameObject;
                _bgs[i] = bg;
            }
            
            _recipeGrids = new MyRecipeGrid[4];
            for (int i = 0; i < 4; ++i)
            {
                GameObject goRecipe = _transform.Find("Recipes/Recipe_" + (i + 1)).gameObject;
                MyRecipeGrid grid = new MyRecipeGrid(goRecipe);
                ClickTrigger trigger = ClickTrigger.Get(grid.GetTransform());
                trigger.AddClickListener(OnRecipeGirdClick, i);
                _recipeGrids[i] = grid;
            }
            
            _triggerOrder = ClickTrigger.Get(_transform.Find("Button_Order"));
            _triggerOrder.AddClickListener(OnOrderClick);
            _triggerHot = ClickTrigger.Get(_transform.Find("Button_Hot"));
            _triggerHot.AddClickListener(OnHotClick);
            _triggerInfo = ClickTrigger.Get(_transform.Find("Button_Info"));
            _triggerInfo.AddClickListener(OnInfoClick);

            _textMoney = _transform.Find("Text_Money").GetComponent<Text>();
            Update();

            RecipeManager.instance.RecipeUpdateEvent += Update;
        }

        protected override void OnDispose()
        {
            RecipeManager.instance.RecipeUpdateEvent -= Update;
            for (var i = 0; i < _recipeGrids.Length; i++)
            {
                _recipeGrids[i].Dispose();
            }
            _recipeGrids = null;
        }

        private void Update()
        {
            DateTime now = DateTime.Now;
            int hour = now.Hour;
            RecipeType recipeType = RecipeType.Night;
            if (hour > 5 && hour <= 10)
            {
                recipeType = RecipeType.Morning;
            }
            else if (hour > 10 && hour <= 16)
            {
                recipeType = RecipeType.Noon;
            }
            for (var i = 0; i < _bgs.Length; i++)
            {
                _bgs[i].SetActive(i == (int)recipeType);
            }

            _textMoney.text = AccountManager.instance.GetCurrentAccount().money.ToString();

            List<Recipe> listRecipe = RecipeManager.instance.GetListRecipe();
            for (var i = 0; i < listRecipe.Count; i++)
            {
                _recipeGrids[i].SetRecipe(listRecipe[i]);
            }
            for (var i = listRecipe.Count; i < _recipeGrids.Length; ++i)
            {
                _recipeGrids[i].SetRecipe(null);
            }
        }

        private void OnRecipeGirdClick(params object[] args)
        {
            int index = (int) args[0];
            MyRecipeGrid grid = _recipeGrids[index];
            Recipe recipe = grid.GetRecipe();
            if (recipe == null)
            {
                new CreateRecipeView().Show();
            }
            else
            {
                RecipeInfoView recipeInfoView = new RecipeInfoView();
                recipeInfoView.SetRecipeId(recipe.id);
                recipeInfoView.Show();
            }
        }

        private void OnRecipeClick(params object[] args)
        {
            
        }
        
        private void OnOrderClick(params object[] args)
        {
            new OrderView().Show();
            Close();
        }
        
        private void OnHotClick(params object[] args)
        {
            new HotRecipeView().Show();
            Close();
        }
        
        private void OnInfoClick(params object[] args)
        {
            new AccountView().Show();
            Close();
        }
        
        
        private class MyRecipeGrid
        {
            private GameObject _gameObject;
            private RectTransform _transform;

            private RecipeDishIconProxy _iconProxy;
            private GameObject _goLabelContainer;
            private GameObject[] _goLabels;
            private Text _textName;

            private Recipe _recipe;

            public MyRecipeGrid(GameObject gameObject)
            {
                _gameObject = gameObject;
                _transform = (RectTransform) _gameObject.transform;
                
                _iconProxy = new RecipeDishIconProxy();
                _iconProxy.AddTo(_transform.Find("Icon"));

                _textName = _transform.Find("Text").GetComponent<Text>();

                _goLabelContainer = _transform.Find("Labels").gameObject;
                _goLabels = new GameObject[3];
                for (int i = 0; i < 3; ++i)
                {
                    GameObject goLabel = _transform.Find("Labels/Label_" + (i + 1)).gameObject;
                    _goLabels[i] = goLabel;
                }
                
                Update();
            }

            public void Dispose()
            {
                _iconProxy.Dispose();
            }

            public Recipe GetRecipe()
            {
                return _recipe;
            }

            public RectTransform GetTransform()
            {
                return _transform;
            }

            public void SetRecipe(Recipe recipe)
            {
                _recipe = recipe;
                Update();
            }

            private void Update()
            {
                if (_recipe == null)
                {
                    _textName.text = string.Empty;
                    _iconProxy.Clear();
                    _goLabelContainer.SetActive(false);
                }
                else
                {
                    _goLabelContainer.SetActive(true);
                    for (var i = 0; i < _goLabels.Length; i++)
                    {
                        _goLabels[i].SetActive(i == (int)_recipe.recipeType);
                    }
                    _iconProxy.SetIconId(_recipe.iconId);
                    _textName.text = _recipe.name;
                }
            }
        }
    }
}